package com.feib.soeasy.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.CommissionCodeTableDao;
import com.feib.soeasy.dao.Soez5009Dao;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.Soez5009Service;
import com.feib.soeasy.util.CodeTableUtil;
import com.feib.soeasy.util.UserUtil;



@Service("soez5009Service")
public class Soez5009ServiceImpl extends BaseServiceImpl implements Soez5009Service {


	protected Logger logger = LoggerFactory.getLogger(this.getClass());


	@Resource(name="soez5009Dao")
	private Soez5009Dao soez5009Dao;


	@Resource(name="commissionCodeTableDao")
	public CommissionCodeTableDao commissionCodeTableDao;


	/*  取得FeedbackLog (總計明細) */
	public PaginatedList<Map<String, Object>> queryTransferLogByGroup(Group group, String channels[],
			Date dateFrom, Date dateTo,PaginatedList<Map<String, Object>> paginationConfig) {

		if(null==group)
			group=new Group();

		if (!UserUtil.isBankGroup())
		{
			group=UserUtil.getUserGroup();
		}

		PaginatedList<Map<String, Object>> fbLogs = soez5009Dao.queryTransferLog(group, channels, dateFrom, dateTo, paginationConfig);

		if(fbLogs.getPaginatedList()== null || fbLogs.getPaginatedList().size() == 0)
			return null;

		/* 效能提升  超商／郵局傳輸統計查詢  總計記數 */
		/* 設定複製用PaginatedList 再對資料作處理 不然 foreach的狀況下不能對來源檔案作操作  */
		PaginatedList<Map<String, Object>> results = new PaginatedList<Map<String, Object>>();				
		results.setFullListSize(fbLogs.getFullListSize());
		results.setPageSize(fbLogs.getPageSize());
		results.setPageNumber(fbLogs.getPageNumber());
		results.setSortCriterion(fbLogs.getSortCriterion());
		results.setSortDirection(fbLogs.getSortDirection());
		/* 效能提升  超商／郵局傳輸統計查詢  總計記數 */
		Long totalCount = new Long(0);
		Long totalAmount = new Long(0);

	
		List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
		for(Map<String, Object> fblog:fbLogs.getPaginatedList())
		{	

				//個別Code統計值
				Long codeCount = new Long(0);
				Long codeAmount = new Long(0);


				//計算手續費
				 codeAmount = ((BigDecimal)fblog.get("realAmount")).longValue();// REAL_AMOUNT
				 codeCount = ((BigDecimal)fblog.get("totalCount")).longValue();//TOTAL_COUNT

				//加統計值
				totalCount += codeCount;
				totalAmount += codeAmount;

				fblog.put("allCount", totalCount);
				fblog.put("allAmount", totalAmount);

				/*計算總計*/
				temp.add(fblog);
		}
		

		results.setPaginatedList(temp);
		/* 清除資源*/
		fbLogs.getPaginatedList().clear();
		fbLogs=null;

		return results;
	}
	
	/**
	 *  取得FeedbackLog (總計明細) */
	public PaginatedList<Map<String, Object>> queryGroupFeedbackLogByChannel(Group group, String channels[],
			Date dateFroms[], Date dateTos[],PaginatedList<Map<String, Object>> paginationConfig) {
		/*
		 *	畫面上的總計放大鏡
		 *	應該要傳所查詢的通路跟各通路的時間
		 */
		Map<String, String> channelCode=new HashMap<String, String>();

		channelCode.put( "7111111","D");
		channelCode.put("TFM","E" );
		channelCode.put( "HILIFE","F");
		channelCode.put( "OKCVS","G");
		channelCode.put("POST","H");



		if(null==group)
			group=new Group();

		if (!UserUtil.isBankGroup())
		{
			group=UserUtil.getUserGroup();
		}

		PaginatedList<Map<String, Object>> fbLogs = soez5009Dao.queryTotalGroupFeedbackLog(group, channels, dateFroms, dateTos, paginationConfig);

		if(fbLogs.getPaginatedList()== null || fbLogs.getPaginatedList().size() == 0)
			return null;

		/* 效能提升  超商／郵局傳輸統計查詢  總計記數 */
		/* 設定複製用PaginatedList 再對資料作處理 不然 foreach的狀況下不能對來源檔案作操作  */
		PaginatedList<Map<String, Object>> results = new PaginatedList<Map<String, Object>>();				
		results.setFullListSize(fbLogs.getFullListSize());
		results.setPageSize(fbLogs.getPageSize());
		results.setPageNumber(fbLogs.getPageNumber());
		results.setSortCriterion(fbLogs.getSortCriterion());
		results.setSortDirection(fbLogs.getSortDirection());

		List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
		for(Map<String, Object> fblog:fbLogs.getPaginatedList())
		{	

			String channel = (String)fblog.get("channel");
			String code = (String)fblog.get("code");
			/* 改善效能 資料改成從db串 */
			/* 畫面上有可能沒傳代碼,所以代碼來源要從資料來  ex:抓整個通路 */		
			/*超商欄位*/
			String  codeName = CodeTableUtil.getInstance().getCodeName("CHANEL", channelCode.get(channel)) ;
			fblog.put("codeName", codeName);		
			/*代號*/
			fblog.put("commissionCode", code);
			
			fblog.put("gpname", (String)fblog.get("gname"));
			
			
			/* 總計明細用的總計 顯示在最下方,最後一頁再顯示 */
			if(fbLogs.isLastPage()){	
				@SuppressWarnings("unchecked")
				Map<String, Object> total=(Map<String, Object>) fbLogs.getTotalObject();
				fblog.put("totalCount", total.get("totalCount"));
				fblog.put("totalAmount", ((BigDecimal)total.get("totalAmount")).longValue());
				fblog.put("totalFee", ((BigDecimal)total.get("totalFee")).longValue());
			}
			/*計算總計*/
			temp.add(fblog);

		}

		results.setPaginatedList(temp);
		/* 清除資源*/
		fbLogs.getPaginatedList().clear();
		fbLogs=null;

		return results;
	}

}
